package com.drug.lib.util;


import android.content.Context;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.drug.lib.R;
import com.drug.lib.base.BaseApplication;

import java.lang.ref.WeakReference;


/**
 * Created by Administrator on 2017/4/25.
 */

public class ToastUtil {

    private static final String TAG = "ToastUtil";

    // 存在异议的Toast提示
    private static final boolean blnCertainToast = false;

    private static WeakReference<Toast> sToast;

    /**
     * @param text
     * @param duration
     * @param gravity
     */
    private static void show(String text, int duration, int gravity) {
        if (BaseApplication.getContext() != null && !TextUtils.isEmpty(text)) {
            Toast toast;
            View toastRoot = ((LayoutInflater) BaseApplication.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(R.layout.toast, null);
            TextView mTextView = toastRoot.findViewById(R.id.toast_text);
            mTextView.setText(text);
            if (sToast == null || (toast = sToast.get()) == null || toast.getView() == null) {
//                toast = new Toast(context.getApplicationContext());
            } else {
                if (sToast.get() != null)
                    sToast.get().cancel();
//                toast = new Toast(context.getApplicationContext());
            }
            toast = Toast.makeText(BaseApplication.getContext().getApplicationContext(), text, duration);
            toast.setView(toastRoot);
            sToast = new WeakReference<>(toast);

            int height = DensityUtil.getScreenHeight();
            toast.setGravity(gravity, 0, height / 8);
            toast.setDuration(duration);
            toast.show();
        }
    }


    /**
     * Duration:LENGTH_SHORT，Gravity: BOTTOM
     *
     * @param text
     */
    public static void show2Center(String text) {
        show2Center(text, Toast.LENGTH_SHORT);
    }

    public static void show2Center(int resId) {
        show2Center(BaseApplication.getContext().getResources().getText(resId).toString(), Toast.LENGTH_SHORT);
    }

    public static void show2CenterTemp(int resId) {
        if (blnCertainToast) {
            show2Center(BaseApplication.getContext().getResources().getText(resId).toString(), Toast.LENGTH_SHORT);
        }
    }

    public static void show2Center(int resId, int duration) {
        show2Center(BaseApplication.getContext().getResources().getText(resId).toString(), duration);
    }

    /**
     * @param text
     * @param duration
     */
    public static void show2Center(String text, int duration) {
        show(text, duration, Gravity.CENTER);
    }

    public static void show2Bottom(String text, int duration) {
        show(text, duration, Gravity.BOTTOM);
    }

    public static void show2Bottom(String text) {
        show(text, Toast.LENGTH_SHORT, Gravity.BOTTOM);
    }

    public static void show2Bottom(int resId) {
        show(BaseApplication.getContext().getString(resId), Toast.LENGTH_SHORT, Gravity.BOTTOM);
    }

    /**
     * Duration:LENGTH_SHORT，Gravity: TOP
     *
     * @param text
     */
    public static void show2Top(String text) {
        show2Top(text, Toast.LENGTH_SHORT);
    }

    public static void show2Top(int textResid) {
        show2Top(BaseApplication.getContext().getString(textResid), Toast.LENGTH_SHORT);
    }

    /**
     * Gravity: TOP
     *
     * @param text
     * @param duration
     */
    public static void show2Top(String text, int duration) {
        show(text, duration, Gravity.TOP);
    }

    /**
     * Duration:LENGTH_SHORT
     *
     * @param text
     * @param gravity
     */
    public static void show(String text, int gravity) {
        show(text, Toast.LENGTH_SHORT, gravity);
    }

    /**
     * 显示Toast（显示到底部，时间为 Toast.LENGTH_SHORT）
     *
     * @param text 文本内容
     */
    public static void show(String text) {
        show(text, Toast.LENGTH_LONG, Gravity.FILL_HORIZONTAL | Gravity.BOTTOM);
    }

    /**
     * 显示Toast（显示到底部，时间为 Toast.LENGTH_SHORT）
     *
     * @param resId 文本资源Id
     */
    public static void show(int resId) {
        show(BaseApplication.getContext().getString(resId), Toast.LENGTH_LONG, Gravity.FILL_HORIZONTAL | Gravity.BOTTOM);
    }

    public static void cancel() {
        Toast toast;
        if (sToast != null && (toast = sToast.get()) != null) {
            toast.cancel();
        }
    }

}
